'use strict';

exports.main = async (event, context) => {
	const { tripId, status, userId } = event;
	
	if (!userId) {
		return {
			code: -1,
			message: '用户未登录'
		}
	}
	
	if (!tripId) {
		return {
			code: -1,
			message: '行程ID不能为空'
		}
	}
	
	if (status === undefined) {
		return {
			code: -1,
			message: '状态不能为空'
		}
	}
	
	const db = uniCloud.database();
	const tripCollection = db.collection('trips');
	
	try {
		// 检查行程是否存在且属于当前用户
		const tripRes = await tripCollection
			.where({
				_id: tripId,
				userId: userId
			})
			.get();
			
		if (tripRes.data.length === 0) {
			return {
				code: -1,
				message: '行程不存在'
			}
		}
		
		// 更新行程状态
		await tripCollection.doc(tripId).update({
			status: status,
			updateTime: Date.now()
		});
		
		return {
			code: 0,
			message: '更新成功'
		}
	} catch (error) {
		return {
			code: -1,
			message: '更新失败'
		}
	}
}; 